package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chixing.entity.Product;
import com.chixing.entity.ProductImg;
import com.chixing.entity.VO.ProductImgVO;
import com.chixing.mapper.ProductImgMapper;
import com.chixing.mapper.ProductMapper;
import com.chixing.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author smith
 * @since 2025-09-22
 */
@Service
public class ProductServiceImpl  implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductImgMapper productImgMapper;
    @Override
    public Result getAllPage(Integer pageNumber, String proName, Float MinPrice, Float MaxPrice) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        if (pageNumber == null)
            pageNumber = 1;
        if (proName!=null)
            wrapper.like("pro_name",proName);
        if (MinPrice!=null)
            wrapper.ge("pro_price",MinPrice);
        if (MaxPrice!=null)
            wrapper.le("pro_price",MaxPrice);
        Page<Product> page = new Page<>(pageNumber,4);
        page = productMapper.selectPage(page, wrapper);
        if (page.getTotal()>0)
            return Result.getSuccess(page);
        return Result.getFail(null);
    }

    @Override
    public Result getByIdDetail(Integer id) {
        Product product = productMapper.selectById(id);
        if (product!=null) {
            QueryWrapper<ProductImg> wrapper = new QueryWrapper<>();
            wrapper.eq("pro_id", id);
            List<ProductImg> productImgList = productImgMapper.selectList(wrapper);
            ProductImgVO productImgVO = new ProductImgVO(product, productImgList);
            return Result.getSuccess(productImgVO);
        }
        return Result.getFail(id);
    }
}
